package com.cheboohui.iqs.taiping.direct.v;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.StringUtil;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.taiping.direct.DirectTp;
import com.cheboohui.iqs.taiping.direct.PublicNew;
import com.cheboohui.iqs.taiping.direct.RequestHead;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;

/**
 * V03-确认投保----该接口初步设计根据用户提供的信息，然后通过其他接口完善数据集信息，将投保信息保存于核心，同时将投保单和状态信息返回给用户。
 * 
 * @author xielonghai
 * @version 2016-07-08
 */
public class V03Request {

	/**
	 * 确认投保接口方法
	 * 
	 * @param session
	 *            会话ID--初始为空,后续调用为上次request返回
	 * @param param
	 *            请求参数json
	 * @return
	 */
	public static ResTaiPing requestV03(String session, JSONObject param) {
		String value = Config.value + ":主接口-确认投保";
		String[] key = { "risk" // 险种信息（risk）
		};

		return DirectTp.request(value, PublicNew.REQUEST_URL + "/insConfirm", getRequest(session, param), key);
	}

	/**
	 * 获取--V03-确认投保------请求参数
	 * 
	 * @param session
	 *            会话ID--初始为空,后续调用为上次request返回
	 * @param param
	 *            请求参数json
	 * @return
	 */
	public static String getRequest(String session, JSONObject param) {
		JSONObject head = RequestHead.getHead(session);
		String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		request = request + "\n" + "<request>";
		request = request + "\n" + "<head>";

		request = request + "\n\t" + "<msgId>" + head.getString("msgId") + "</msgId>";
		request = request + "\n\t" + "<timeStamp>" + head.getString("timeStamp") + "</timeStamp>";
		request = request + "\n\t" + "<session>" + head.getString("session") + "</session>";
		request = request + "\n\t" + "<userCode>" + head.getString("userCode") + "</userCode>";
		request = request + "\n\t" + "<clientName>" + head.getString("clientName") + "</clientName>";

		request = request + "\n" + "</head>";
		request = request + "\n" + "<body>";

		/** 车主信息 **/
		request = request + "\n\t" + "<carOwner>";
		request = request + "\n\t\t" + "<name>" + param.getJSONObject("carOwner").getString("name") + "</name>";// 车主姓名
		request = request + "\n\t\t" + "<identifyNumber>" + param.getJSONObject("carOwner").getString("identifyNumber")
				+ "</identifyNumber>";// 车主身份证号码
		request = request + "\n\t" + "</carOwner>";

		/** 投保人信息 **/
		request = request + "\n\t" + "<applicant>";
		request = request + "\n\t\t" + "<appliName>" + param.getJSONObject("applicant").getString("appliName")
				+ "</appliName>";// 投保人名称
		request = request + "\n\t\t" + "<appliAddress>" + param.getJSONObject("applicant").getString("appliAddress")
				+ "</appliAddress>";// 投保人地址
		request = request + "\n\t\t" + "<identifyNumber>" + param.getJSONObject("applicant").getString("identifyNumber")
				+ "</identifyNumber>";// 投保人证件号码
		request = request + "\n\t\t" + "<mobilePhone>" + param.getJSONObject("applicant").getString("mobilePhone")
				+ "</mobilePhone>";// 移动电话
		if (!StringUtil.isNulls(param.getJSONObject("applicant").getString("email")))
			request = request + "\n\t\t" + "<email>" + param.getJSONObject("applicant").getString("email") + "</email>";// 北京地区需要传送
		request = request + "\n\t" + "</applicant>";

		/** 被保险人信息 **/
		request = request + "\n\t" + "<insured>";
		request = request + "\n\t\t" + "<insuredName>" + param.getJSONObject("insured").getString("insuredName")
				+ "</insuredName>";// 被保人名称
		request = request + "\n\t\t" + "<insuredAddress>" + param.getJSONObject("insured").getString("insuredAddress")
				+ "</insuredAddress>";// 被保人证件号码
		request = request + "\n\t\t" + "<identifyNumber>" + param.getJSONObject("insured").getString("identifyNumber")
				+ "</identifyNumber>";// 移动电话
		request = request + "\n\t\t" + "<insuredMobilePhone>"
				+ param.getJSONObject("insured").getString("insuredMobilePhone") + "</insuredMobilePhone>";// 被保人地址
		request = request + "\n\t" + "</insured>";

		/** 配送信息 **/
		request = request + "\n\t" + "<dispatch>";
		request = request + "\n\t\t" + "<name>" + param.getJSONObject("dispatch").getString("name") + "</name>";// 收件人名称
		request = request + "\n\t\t" + "<address>" + param.getJSONObject("dispatch").getString("address")
				+ "</address>";// 收件人地址
		request = request + "\n\t\t" + "<mobilePhone>" + param.getJSONObject("dispatch").getString("mobilePhone")
				+ "</mobilePhone>";// 移动电话
		request = request + "\n\t" + "</dispatch>";

		/** 订单信息 **/
		request = request + "\n\t" + "<order>";
		request = request + "\n\t\t" + "<orderNo>" + param.getJSONObject("order").getString("orderNo") + "</orderNo>";// 订单号
		request = request + "\n\t" + "</order>";

		request = request + "\n" + "</body>";
		request = request + "\n" + "</request>";
		return request;
	}

}
